package com.example.demo.util;

import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Div;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by zhanglei on 2024/3/16.
 */
public class Pdf7Utils {

    /**
     * 字体存放的跟路径，默认为'C:\Windows\Fonts\'
     */
    private static final String FONT = "C:\\Windows\\Fonts\\simsun.ttc,0";
    /**
     * 纸张大小
     */
    private static Rectangle RECTANGLE = PageSize.A4;


    public static Cell createCell(String value,Style style, HorizontalAlignment alignment,int rowspan, int colspan,  Boolean boderFlag){

        Cell cell = new Cell(rowspan, colspan);
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.setHorizontalAlignment(alignment);
        cell.add(new Paragraph(value));
        cell.addStyle(style);
        if(boderFlag != null && !boderFlag){
            cell.setBorder(Border.NO_BORDER);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);

        }
        return cell;
    }

    public static Cell createCell(String value,Style style, HorizontalAlignment alignment,int rowspan,int colspan){

        Cell cell = new Cell(rowspan, colspan);
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.setHorizontalAlignment(alignment);
        cell.add(new Paragraph(value));
        cell.addStyle(style);
        return cell;

    }

    public static Cell createCell(String value,Style style, HorizontalAlignment alignment){

        Cell cell = new Cell();
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.setHorizontalAlignment(alignment);
        cell.add(new Paragraph(value));
        cell.addStyle(style);
        return cell;

    }

    //获取字体大小
    /*private static Font getFont(BaseFont baseFont, String value) {
        int textBoxWidth = 45;
        int fontSize = 10;
        float textWidth = baseFont.getWidthPoint(value, fontSize);
        // 文本框高度只够写一行，并且文字宽度大于文本框宽度，则缩小字体
        while (textWidth > textBoxWidth) {
            fontSize--;
            textWidth = baseFont.getWidthPoint(value, fontSize);
        }
        return PdfUtils.setFont(fontSize);
    }*/

    public static void main(String[] args) throws Exception {


        PdfFont font = PdfFontFactory.createFont(FONT);
        // 创建PDF文档
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("D:\\itext-table.pdf"));
        // 创建文档对象
        Document document = new Document(pdfDocument);

        Paragraph paragraph = new Paragraph("红字发票信息确认单");
        //设置文字居中
        Style titleStyle = new Style().setFont(font).setFontSize(20).setTextAlignment(TextAlignment.CENTER).setPaddingTop(15.0f).setPaddingBottom(10.0f);
        Style timeStyle = new Style().setFont(font).setFontSize(14).setTextAlignment(TextAlignment.LEFT);
        Style textStyle = new Style().setFont(font).setFontSize(10).setTextAlignment(TextAlignment.CENTER);

        paragraph.addStyle(titleStyle);
        document.add(paragraph);

        // 创建表格
        float[] columnWidths = new float[]{45,95,110,45,70,55,55};
        Table table = new Table(columnWidths);
        // 设置表格宽度100%
        table.setWidth(UnitValue.createPercentValue(100));
        // 设置表格标题

        // 添加表头单元格，上面设置了四列，超过四列会自动换行
        table.addCell(createCell("填开日期：2024年03月01日", timeStyle,HorizontalAlignment.LEFT,1,7,false));
        table.addCell(createCell("销售方", textStyle,HorizontalAlignment.CENTER,2,1));
        table.addCell(createCell("纳税人名称（销方）", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("测试测试企业名称", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("购买方", textStyle, HorizontalAlignment.CENTER,2,1));
        table.addCell(createCell("纳税人名称（购方）", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("测试购方企业名称", textStyle, HorizontalAlignment.CENTER,1,3));

        table.addCell(createCell("统一社会信用代码/纳税人识别号（销方）", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("91440604MA52J0J10H", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("统一社会信用代码/纳税人识别号（购方）", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("911136587421536541", textStyle, HorizontalAlignment.CENTER,1,2));

        int num = 40;


        float[] columnWidths2 = new float[]{45};
        Table table2 = new Table(columnWidths2);
        table2.setWidth(UnitValue.createPercentValue(100));
        table2.addHeaderCell(createCell("开具\r\n红字\r\n发票\r\n确认\r\n信息\r\n内容", textStyle, HorizontalAlignment.CENTER));

        Cell cell = new Cell(num+3, 1);
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
        cell.addStyle(textStyle);
        cell.add(table2);

        Cell cell11 = createCell("开具\r\n红字\r\n发票\r\n确认\r\n信息\r\n内容", textStyle, HorizontalAlignment.CENTER,  num + 3,1);
        table.addCell(cell);
        table.addCell(createCell("项目名称", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("数量", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("单价", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("金额", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("税率/征收率", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("税额", textStyle, HorizontalAlignment.CENTER));

        //明细
        for(int i=0;i<num;i++){
            table.addCell(createCell("*蔬菜*豆芽"+i, textStyle, HorizontalAlignment.CENTER));
            table.addCell(createCell(i+"", textStyle, HorizontalAlignment.CENTER));
            /*String value = "1.234789046789";
            Font font = getFont(baseFont, value);
            Cell cell = createCell(value, font);
            table.addCell(cell);*/

            table.addCell(createCell("1.2", textStyle, HorizontalAlignment.CENTER));
            table.addCell(createCell("1.2", textStyle, HorizontalAlignment.CENTER));
            table.addCell(createCell("0%", textStyle, HorizontalAlignment.CENTER));
            table.addCell(createCell("0.00", textStyle, HorizontalAlignment.CENTER));

        }
        table.addCell(createCell("合计", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("----", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("----", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("6.00", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("----", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("6.00", textStyle, HorizontalAlignment.CENTER));



        Cell cell2 = new Cell(1,6);
        // 水平、垂直居中
        cell2.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell2.setHorizontalAlignment(HorizontalAlignment.LEFT);

        Paragraph para1 = new Paragraph("").addStyle(textStyle).setVerticalAlignment(VerticalAlignment.MIDDLE).setHorizontalAlignment(HorizontalAlignment.LEFT);
        Paragraph para2 = new Paragraph("").addStyle(textStyle).setVerticalAlignment(VerticalAlignment.MIDDLE).setHorizontalAlignment(HorizontalAlignment.LEFT);
        Paragraph para3 = new Paragraph("").addStyle(textStyle).setVerticalAlignment(VerticalAlignment.MIDDLE).setHorizontalAlignment(HorizontalAlignment.LEFT);
        // 添加文本和图片到段落
        para1.add("一、录入方身份：\r\n");
        para1.add("  1.销售方□☑  2.购买方□☑  \r\n");
        para1.add("二、冲红原因：\r\n");
        para1.add("  1.开票有误□☑  2.销货退回□☑  3.服务终止□☑  4.销售折让□☑  \r\n");
        para1.add("三、对应蓝字发票抵扣增值税销项税额情况：\r\n");
        para1.add(" 1.已抵扣□☑  2.未抵扣□☑  \r\n");
        para1.add(" 对应蓝字发票的代码： 号码：");
        para2.add("24442000000000071607\n").setUnderline(0.8f, -1);
        para3.add("四、是否涉及数量（仅限成品油、机动车等业务填写）\r\n");
        para3.add("  涉及销售数量□☑  仅涉及销售金额□☑  \r\n");
        para3.add("\r\n");
        cell2.add(para1);
        cell2.add(para2);
        cell2.add(para3);
        table.addCell(cell2);

        table.addCell(createCell("红字发票信息确认单编号", textStyle, HorizontalAlignment.CENTER));
        table.addCell(createCell("24442000000000071607", textStyle, HorizontalAlignment.LEFT,1,6));

        // 添加表格到PDF文档
        document.add(table);
        // 关闭文档
        document.close();
        pdfDocument.close();


    }


}
